package com.project.forlearningdemo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.project.forlearningdemo.constant.ResponseTool;
import com.project.forlearningdemo.dao.CommentMapper;
import com.project.forlearningdemo.dao.ResourceMapper;
import com.project.forlearningdemo.pojo.Comment;
import com.project.forlearningdemo.pojo.Resource;
import com.project.forlearningdemo.pojo.vo.Info;
import com.project.forlearningdemo.pojo.vo.ResponseInfo;
import com.project.forlearningdemo.pojo.vo.StarNumPo;
import com.project.forlearningdemo.service.CommentService;
import com.project.forlearningdemo.utils.PageInfoUtil;
import com.project.forlearningdemo.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.codec.ResourceDecoder;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    PageInfoUtil<Comment> pageInfoUtil;

    Info<Comment> commentInfo = new Info<>();

    /**
     * 添加评论
     */
    @Override
    public String addComment(String comment) {
        //格式转换
        Comment comment1 = (Comment) StringUtils.tranJSON(comment, new Comment());
        //添加评论
        if(commentMapper.saveComment(comment1)){
            //根据星级来评星级
            //确定用户评论的星级
            Integer starNum = comment1.getStarNum();
            //如果评论未评星级
            if(starNum != null){
                //确定资源编号
                Integer resourceId = comment1.getResourceId();
                //统计资源评论等级信息
                StarNumPo starNumPo = commentMapper.queryStarNumPo(resourceId);
                //总星数
                int starSum = starNumPo.getStarSum();
                //总人数
                int userSum = starNumPo.getUserSum();
                //平均数
                int newStarNum = new BigDecimal(starSum/userSum).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
                //加入到资源中
                Resource resource = new Resource();
                resource.setResourceId(resourceId);
                resource.setStarNum(newStarNum);
                resourceMapper.updateResource(resource);
                //修改结束
            }

            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.ADD_SUCCESS.code,ResponseTool.ADD_SUCCESS.msg,comment));
        }

        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.ADD_FAIL.code,ResponseTool.ADD_FAIL.msg,false));
    }

    @Override
    public String queryComment(String idp) {
        JSONObject jsonObject = JSONObject.parseObject(idp);
        Integer commentId = jsonObject.getInteger("commentId");
        Integer resourceId = jsonObject.getInteger("resourceId");
        Integer comPageNum = jsonObject.getInteger("comPageNum"); //评论的当前页数

        if(comPageNum == null) return JSON.toJSONString(new ResponseInfo<>(500, "评论的当前页数comPageNum不能为空", false));
        //查询
        List<Comment> commentList = commentMapper.selectComment(commentId, resourceId);

        commentInfo = pageInfoUtil.resetPage(comPageNum, 5, commentList, commentInfo);
        if(commentList == null){
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS_ALL.code, ResponseTool.QUERY_SUCCESS_ALL.msg, StringUtils.list));
        }

        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.QUERY_SUCCESS.code, ResponseTool.QUERY_SUCCESS.msg, commentInfo));
    }

    @Override
    public String deleteComment(int commentId) {
        if(commentMapper.deleteComment(commentId)){
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_SUCCESS.code, ResponseTool.DELETE_SUCCESS.msg, true));
        }
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DELETE_FAIL.code, ResponseTool.DELETE_FAIL.msg, false));
    }

    @Override
    public String updateComment(String comment) {
        Comment comment1 = (Comment) StringUtils.tranJSON(comment, new Comment());

        commentMapper.updateComment(comment1);
        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.UPDATE_SUCCESS.code, ResponseTool.UPDATE_SUCCESS.msg, true));
    }
}
